package com.tubi.edu.info.manager.dao;

import com.tubi.edu.info.manager.domain.Student;

import java.util.ArrayList;

// 库管
public class OtherStudentDao implements BaseStudentDao {
    //1。创建一个学习对象数组
    private static ArrayList<Student>  stus = new ArrayList<>();  //前面加static 属于静态成员对象共享
     static {
         Student stu1 = new Student("001","ronglei","21","1991-12-12");
         Student stu2 = new Student("003","rongl3ei","21","1991-12-12");
         Student stu3 = new Student("002","rongle32i","213","1991-12-12");
         stus.add(stu1);
         stus.add(stu2);
         stus.add(stu3);
     }
    // 添加学生
    public boolean addStudent(Student stu) {
        //2。 添加学学生到数组
         stus.add(stu);
         return true;
    }
    //查看学生
    public Student[] findAllStudent() {
         Student[] students = new Student[stus.size()];
        for (int i = 0; i < students.length; i++) {
            students[i] = stus.get(i);
        }
        return students;
    }
    //删除学生
    public void deleteStudentById(String id) {
        //查找id所在数组的位置
        int index = getIndex(id);
        stus.remove(index);

    }
    //查找id所在数组的位置
    public int getIndex ( String id){
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            Student stu = stus.get(i);
            if(stu != null && stu.getId().equals(id)){
                index = i;
            }
        }
        return index;
    }
    //修改学生
    public void updateStudent(String id, Student stu) {
        //查找 id位置
        int index = getIndex(id);
        // 进行替换
        stus.set(index, stu);
    }
}
